﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPFByCoreApp
{
    /// <summary>
    /// Interaction logic for TestTAPWindow.xaml
    /// </summary>
    public partial class TestTAPWindow : Window
    {
        public TestTAPWindow()
        {
            InitializeComponent();
        }

        private void btnTAP_Click(object sender, RoutedEventArgs e)
        {
            tb1.Text += $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} main thread {Thread.CurrentThread.ManagedThreadId} start" + System.Environment.NewLine;
            Calculate();
            tb1.Text += $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} main thread {Thread.CurrentThread.ManagedThreadId} end" + System.Environment.NewLine;
        }



        private void btnTAPNoUI_Click(object sender, RoutedEventArgs e)
        {

            Debug.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} main thread {Thread.CurrentThread.ManagedThreadId} start");
            Calculate1();
            Debug.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} main thread {Thread.CurrentThread.ManagedThreadId} end");
        }

        public async void Calculate()
        {
            tb1.Text += $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} calculate method thread {Thread.CurrentThread.ManagedThreadId} start" + System.Environment.NewLine;
            //await Task.Run(() =>
            // {
            //     string startStr = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} calculate task thread {Thread.CurrentThread.ManagedThreadId} start";
            //     this.Dispatcher.Invoke((Action)delegate
            //     {
            //         tb1.Text += startStr + System.Environment.NewLine;
            //     });
            //     Thread.Sleep(10000);
            //     string endStr = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} calculate task thread {Thread.CurrentThread.ManagedThreadId} end";
            //     this.Dispatcher.Invoke((Action)delegate
            //     {
            //         tb1.Text += endStr + System.Environment.NewLine;
            //     });
            // });

            string str = await Task.Run(() =>
              {
                  string startStr = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} calculate task thread {Thread.CurrentThread.ManagedThreadId} start";
                  this.Dispatcher.Invoke((Action)delegate
                  {
                      tb1.Text += startStr + System.Environment.NewLine;
                  });

                  Thread.Sleep(10000);

                  return $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} calculate task thread {Thread.CurrentThread.ManagedThreadId} end";

              }).ConfigureAwait(true);

            tb1.Text += str + System.Environment.NewLine;



            tb1.Text += $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} calculate method thread {Thread.CurrentThread.ManagedThreadId} end" + System.Environment.NewLine;
        }


        public async void Calculate1()
        {
            Debug.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} calculate method thread {Thread.CurrentThread.ManagedThreadId} start");
            await Task.Run(() =>
            {
                Debug.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} calculate task thread {Thread.CurrentThread.ManagedThreadId} start");
                Thread.Sleep(3000);
                Debug.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} calculate task thread {Thread.CurrentThread.ManagedThreadId} end");
            });

            Debug.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} calculate method thread {Thread.CurrentThread.ManagedThreadId} end");
        }

    }
}
